const express = require("express");

const app = express();

app.use(express.static("./static"));
app.use(express.urlencoded({ extended: false }));

/* 获取视频信息 */
const videoData = require("./db.json");
app.get("/videoDetail", (req, res) => {
	const vid = req.query.vid;
	console.log("vid", vid);
	const videoObj = videoData.videos.find((item) => {
		return item.vid === vid;
	});
	/* console.log(videoObj); */
	if (videoObj) {
		res.send({ code: 200, data: videoObj, message: "成功" });
	} else {
		res.send({ code: 401, message: "失败" });
	}
});

/* 获取评论信息 */
const commentData = require("./comment.json");
app.get("/comment", (req, res) => {
	const vid = req.query.vid;
	const commentObj = commentData.filter((item) => {
		return item.vid === vid;
	});
	res.send({ code: 200, data: commentObj, message: "成功" });
});

// 获取推荐视频数据
app.get("/recommentVideo", (req, res) => {
	const vType = req.query.vType;
	const recommentVideoList = videoData.videos.filter(
		(item) => item.vType === vType
	);
	res.send({ code: 200, data: recommentVideoList, message: "成功" });
});

// 获取用户信息
app.get("/getUserInfo", (req, res) => {
	const uid = req.query.uid;
	const user = videoData.users.find((item) => {
		return item.uid === uid;
	});
	if (user) {
		res.send({ code: 200, data: user, message: "成功" });
	} else {
		res.send({ code: 401, message: "失败" });
	}
});

app.listen(5000, (err) => {
	if (err) console.log(err);
	else {
		console.log("serve is Running");
	}
});
